package com.melody.android.ui.viewModel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.melody.android.R
import com.melody.android.logic.database.dao.SongListDaoImpl
import com.melody.android.logic.database.repository.SongListRepositoryImpl
import com.melody.android.logic.model.RecommendSongList
import com.melody.android.logic.model.Song
import com.melody.android.logic.model.SongList

class SongViewModel : ViewModel() {

    private val songListDao = SongListDaoImpl()
    private val songListRepository = SongListRepositoryImpl(songListDao)

    // 当前正在播放的歌曲(选中的歌曲)
    private val _selectedSong = MutableLiveData<Song>()
    val selectedSong: LiveData<Song>
        get() = _selectedSong
    // 推荐歌单列表
    private val _recommendSongLists = MutableLiveData<List<RecommendSongList>>()
    val recommendSongLists: LiveData<List<RecommendSongList>>
        get() = _recommendSongLists
    // 精选单曲列表
    private val _starSongList = MutableLiveData<List<Song>>()
    val starSongList: LiveData<List<Song>>
        get() = _starSongList
    // 歌曲列表
    private val _songList = MutableLiveData<List<Song>>()
    val songList: LiveData<List<Song>>
        get() = _songList
    // 自定义歌单列表
    private val _songLists = MutableLiveData<List<SongList>>()
    val songLists: LiveData<List<SongList>>
        get() = _songLists

    init {
        initSongs()
        initRecommendSongLists()
        initStarSongs()
        loadSongLists()
    }

    /**
     * 选择播放歌曲
     */
    fun selectSong(song: Song) {
        _selectedSong.value = song
    }

    /**
     * 新增自定义歌单
     * @param songList 待新增的SongList实例对象
     */
    fun addSongList(songList: SongList) {
        songListRepository.addSongList(songList)
        loadSongLists()
    }

    /**
     * 移除自定义歌单
     * @param songList 待移除的SongList实例对象
     */
    fun removeSongList(songList: SongList) {
        songListRepository.removeSongList(songList)
        loadSongLists()
    }

    /**
     * 初始化歌曲列表(本地测试)
     */
    private fun initSongs() {
        val songList = mutableListOf<Song>()

        // Jay
        songList.add(Song(R.drawable.img_song_cover_jay_1, "可爱女人", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jay_1, "完美主义", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jay_2, "星晴", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jay_1, "娘子", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jay_1, "斗牛", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jay_1, "黑色幽默", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jay_2, "伊斯坦堡", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jay_2, "印第安老斑鸠", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jay_2, "龙卷风", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jay_2, "反方向的钟", "周杰伦"))
        // 范特西
        songList.add(Song(R.drawable.img_song_cover_ftx, "爱在西元前", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ftx, "爸我回来了", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ftx, "简单爱", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ftx, "忍者", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ftx, "开不了口", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ftx, "上海一九四三", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ftx, "对不起", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ftx, "威廉古堡", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ftx, "双截棍", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ftx, "安静", "周杰伦"))
        // 范特西-plus
        songList.add(Song(R.drawable.img_song_cover_ftx_plus, "蜗牛", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ftx_plus, "世界末日", "周杰伦"))
        // 八度空间
        songList.add(Song(R.drawable.img_song_cover_bdkj, "半兽人", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_bdkj, "半岛铁盒", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_bdkj, "暗号", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_bdkj, "龙拳", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_bdkj, "火车叨位去", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_bdkj, "分裂", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_bdkj, "爷爷泡的茶", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_bdkj, "回到过去", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_bdkj, "米兰的小铁匠", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_bdkj, "最后的战役", "周杰伦"))
        // 叶惠美
        songList.add(Song(R.drawable.img_song_cover_yhm, "以父之名", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yhm, "懦夫", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yhm, "晴天", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yhm, "三年二班", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yhm, "东风破", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yhm, "你听得到", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yhm, "同一种调调", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yhm, "她的睫毛", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yhm, "爱情悬崖", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yhm, "梯田", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yhm, "双刀", "周杰伦"))
        // 寻找周杰伦
        songList.add(Song(R.drawable.img_song_cover_xzzjl, "轨迹", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_xzzjl, "断了的弦", "周杰伦"))
        // 七里香
        songList.add(Song(R.drawable.img_song_cover_qlx, "我的地盘", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_qlx, "七里香", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_qlx, "借口", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_qlx, "外婆", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_qlx, "将军", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_qlx, "搁浅", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_qlx, "乱舞春秋", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_qlx, "困兽之斗", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_qlx, "园游会", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_qlx, "止战之殇", "周杰伦"))
        // 十一月的肖邦
        songList.add(Song(R.drawable.img_song_cover_syydxb, "夜曲", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_syydxb, "蓝色风暴", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_syydxb, "发如雪", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_syydxb, "黑色毛衣", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_syydxb, "四面楚歌", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_syydxb, "枫", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_syydxb, "浪漫手机", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_syydxb, "逆鳞", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_syydxb, "麦芽糖", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_syydxb, "珊瑚海", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_syydxb, "漂移", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_syydxb, "一路向北", "周杰伦"))
        // 霍元甲
        songList.add(Song(R.drawable.img_song_cover_hyj, "霍元甲", "周杰伦"))
        // 依然范特西
        songList.add(Song(R.drawable.img_song_cover_yrftx, "夜的第七章", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yrftx, "听妈妈的话", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yrftx, "本草纲目", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yrftx, "退后", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yrftx, "红模仿", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yrftx, "心雨", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yrftx, "白色风车", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yrftx, "迷迭香", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_yrftx, "菊花台", "周杰伦"))
        // 我很忙
        songList.add(Song(R.drawable.img_song_cover_whm, "牛仔很忙", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_whm, "彩虹", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_whm, "青花瓷", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_whm, "阳光宅男", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_whm, "蒲公英的约定", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_whm, "无双", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_whm, "我不配", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_whm, "扯", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_whm, "甜甜的", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_whm, "最长的电影", "周杰伦"))
        // 不能说的秘密
        songList.add(Song(R.drawable.img_song_cover_bnsdmm, "不能说的秘密", "周杰伦"))
        // 魔杰座
        songList.add(Song(R.drawable.img_song_cover_mjz, "龙战骑士", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_mjz, "蛇舞", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_mjz, "花海", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_mjz, "魔术先生", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_mjz, "说好的幸福呢", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_mjz, "兰亭序", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_mjz, "流浪诗人", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_mjz, "乔克叔叔", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_mjz, "稻香", "周杰伦"))
        // 跨时代
        songList.add(Song(R.drawable.img_song_cover_ksd, "说了再见", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ksd, "烟花易冷", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ksd, "免费教学录影带", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ksd, "雨下一整晚", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ksd, "我落泪情绪零碎", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ksd, "爱的飞行日记", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ksd, "自导自演", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_ksd, "超人不会飞", "周杰伦"))
        // 惊叹号
        songList.add(Song(R.drawable.img_song_cover_jth, "惊叹号", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jth, "皮影戏", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jth, "Mine Mine", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jth, "你好吗", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jth, "迷魂曲", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jth, "超跑女神", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jth, "世界未末日", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jth, "水手怕水", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jth, "琴伤", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jth, "公主病", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_jth, "疗伤烧肉粽", "周杰伦"))
        // 十二新作
        songList.add(Song(R.drawable.img_song_cover_sexz, "四季列车", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_sexz, "手语", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_sexz, "公公偏头痛", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_sexz, "明明就", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_sexz, "傻笑", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_sexz, "比较大的大提琴", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_sexz, "红尘客栈", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_sexz, "梦想启动", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_sexz, "大笨钟", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_sexz, "哪里都是你", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_sexz, "乌克丽丽", "周杰伦"))
        // 哎呦不错哦
        songList.add(Song(R.drawable.img_song_cover_aybco, "阳明山", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_aybco, "窃爱", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_aybco, "算什么男人", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_aybco, "天涯过客", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_aybco, "怎么了", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_aybco, "一口气全念对", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_aybco, "我要夏天", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_aybco, "手写的从前", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_aybco, "鞋子特大号", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_aybco, "听爸爸的话", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_aybco, "美人鱼", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_aybco, "听见下雨的声音", "周杰伦"))
        // 周杰伦床边故事
        songList.add(Song(R.drawable.img_song_cover_zjlcbgs, "床边故事", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zjlcbgs, "说走就走", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zjlcbgs, "一点点", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zjlcbgs, "前世情人", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zjlcbgs, "英雄", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zjlcbgs, "不该", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zjlcbgs, "土耳其冰淇淋", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zjlcbgs, "告白气球", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zjlcbgs, "Now You See Me", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zjlcbgs, "爱情废柴", "周杰伦"))
        // 最伟大的作品
        songList.add(Song(R.drawable.img_song_cover_zwddzp, "还在流浪", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zwddzp, "说好不哭", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zwddzp, "红颜如霜", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zwddzp, "不爱我就拉倒", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zwddzp, "Mojito", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zwddzp, "错过的烟火", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zwddzp, "等你下课", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zwddzp, "粉色海洋", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zwddzp, "倒影", "周杰伦"))
        songList.add(Song(R.drawable.img_song_cover_zwddzp, "我是如此相信", "周杰伦"))

        // 将歌曲列表赋值给_songList
        _songList.value = songList
    }

    /**
     * 初始化推荐歌单列表(本地测试)
     */
    private fun initRecommendSongLists() {
        val recommendSongLists = mutableListOf<RecommendSongList>()

        recommendSongLists.add(RecommendSongList(R.drawable.img_song_cover_jay_1, "Jay"))
        recommendSongLists.add(RecommendSongList(R.drawable.img_song_cover_ftx, "范特西"))
        recommendSongLists.add(RecommendSongList(R.drawable.img_song_cover_bdkj, "八度空间"))
        recommendSongLists.add(RecommendSongList(R.drawable.img_song_cover_yhm, "叶惠美"))
        recommendSongLists.add(RecommendSongList(R.drawable.img_song_cover_xzzjl, "寻找周杰伦"))
        recommendSongLists.add(RecommendSongList(R.drawable.img_song_cover_qlx, "七里香"))
        recommendSongLists.add(RecommendSongList(R.drawable.img_song_cover_syydxb, "十一月的肖邦"))
        recommendSongLists.add(RecommendSongList(R.drawable.img_song_cover_yrftx, "依然范特西"))
        recommendSongLists.add(RecommendSongList(R.drawable.img_song_cover_whm, "我很忙"))
        recommendSongLists.add(RecommendSongList(R.drawable.img_song_cover_mjz, "魔杰座"))
        recommendSongLists.add(RecommendSongList(R.drawable.img_song_cover_ksd, "跨时代"))
        recommendSongLists.add(RecommendSongList(R.drawable.img_song_cover_jth, "惊叹号"))
        recommendSongLists.add(RecommendSongList(R.drawable.img_song_cover_sexz, "十二新作"))
        recommendSongLists.add(RecommendSongList(R.drawable.img_song_cover_aybco, "哎呦不错哦"))
        recommendSongLists.add(RecommendSongList(R.drawable.img_song_cover_zjlcbgs, "周杰伦床边故事"))
        recommendSongLists.add(RecommendSongList(R.drawable.img_song_cover_zwddzp, "最伟大的作品"))

        _recommendSongLists.value = recommendSongLists
    }

    /**
     * 初始化精选单曲列表(本地测试)
     */
    private fun initStarSongs() {
        val starSongList = mutableListOf<Song>()

        starSongList.add(Song(R.drawable.img_song_cover_xzzjl, "轨迹", "周杰伦"))
        starSongList.add(Song(R.drawable.img_song_cover_ksd, "我落泪情绪零碎", "周杰伦"))
        starSongList.add(Song(R.drawable.img_song_cover_whm, "蒲公英的约定", "周杰伦"))
        starSongList.add(Song(R.drawable.img_song_cover_zwddzp, "倒影", "周杰伦"))
        starSongList.add(Song(R.drawable.img_song_cover_qlx, "搁浅", "周杰伦"))
        starSongList.add(Song(R.drawable.img_song_cover_qlx, "七里香", "周杰伦"))
        starSongList.add(Song(R.drawable.img_song_cover_ftx, "反方向的钟", "周杰伦"))
        starSongList.add(Song(R.drawable.img_song_cover_yhm, "晴天", "周杰伦"))
        starSongList.add(Song(R.drawable.img_song_cover_yhm, "她的睫毛", "周杰伦"))

        _starSongList.value = starSongList
    }

    /**
     * 加载自定义歌单(本地测试)
     */
    private fun loadSongLists() {
        val songLists = mutableListOf<SongList>()

        // 项目内置的歌单(Jay经典歌单)
        songLists.add(SongList(R.drawable.img_song_cover_jay_2, "Jay", 10))
        songLists.add(SongList(R.drawable.img_song_cover_ftx, "范特西", 10))
        songLists.add(SongList(R.drawable.img_song_cover_ftx_plus, "范特西-plus", 3))
        songLists.add(SongList(R.drawable.img_song_cover_bdkj, "八度空间", 10))
        songLists.add(SongList(R.drawable.img_song_cover_yhm, "叶惠美", 11))
        songLists.add(SongList(R.drawable.img_song_cover_xzzjl, "寻找周杰伦", 2))
        songLists.add(SongList(R.drawable.img_song_cover_qlx, "七里香", 10))
        songLists.add(SongList(R.drawable.img_song_cover_syydxb, "十一月的肖邦", 12))
        songLists.add(SongList(R.drawable.img_song_cover_hyj, "霍元甲", 1))
        songLists.add(SongList(R.drawable.img_song_cover_yrftx, "依然范特西", 10))
        songLists.add(SongList(R.drawable.img_song_cover_whm, "我很忙", 10))
        songLists.add(SongList(R.drawable.img_song_cover_bnsdmm, "不能说的秘密", 1))
        songLists.add(SongList(R.drawable.img_song_cover_mjz, "魔杰座", 9))
        songLists.add(SongList(R.drawable.img_song_cover_ksd, "跨时代", 8))
        songLists.add(SongList(R.drawable.img_song_cover_jth, "惊叹号", 11))
        songLists.add(SongList(R.drawable.img_song_cover_sexz, "十二新作", 12))
        songLists.add(SongList(R.drawable.img_song_cover_aybco, "哎呦, 不错哦", 12))
        songLists.add(SongList(R.drawable.img_song_cover_zjlcbgs, "周杰伦床边故事", 10))
        songLists.add(SongList(R.drawable.img_song_cover_zwddzp, "最伟大的作品", 10))
        // 用户自定义的歌单
        val newSongLists = songListRepository.allSongLists
        for (i in 0 until newSongLists.size) {
            songLists.add(SongList(
                R.drawable.img_new_song_list_default_cover,
                newSongLists[i].name,
                newSongLists[i].num
            ))
        }

        _songLists.value = songLists
    }

}